package ranab.jar;

import java.io.File;
import java.util.Enumeration;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;

/**
 * View a jar file - display all the entries.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public
class JarViewer implements Runnable {

    private File mJarFile;
    private JarObserverContainer mObserverCont;


    public JarViewer(File jarFile) {                   
        mJarFile = jarFile;
        mObserverCont = new JarObserverContainer();
    }
    
    /**
     * add observer
     */
    public void addObserver(JarObserver obsr)  {
        mObserverCont.addObserver(obsr);
    }
    
    /**
     * remove observer
     */
    public void removeObserver(JarObserver obsr)  {
        mObserverCont.removeObserver(obsr);
    }
    
    
    /**
     * invoke a new thread
     */
    public void view() {
        Thread th = new Thread(this);
        th.start();
    }

    /**
     * thread starting point - open the jar file
     */
    public void run() {
        mObserverCont.start();
        try {
            JarFile jf = new JarFile(mJarFile);
            mObserverCont.setCount(jf.size());
            Enumeration en = jf.entries();
            while (en.hasMoreElements()) {
                JarEntry je = (JarEntry)en.nextElement();
                mObserverCont.setNext(je);
            }
        } catch (Exception ex) {
            mObserverCont.setError(ex.getMessage());
        } finally {
            mObserverCont.end();
        }
    }

    /**
     * get the jar filename
     */
    public String toString() {
        return mJarFile.getAbsolutePath();
    }

}
